package com.dq.controller.api.merchant.orders;

import com.dq.exception.customException.AdminException;
import com.dq.controller.in.OrgOrderQuery;
import com.dq.controller.in.request.OrgOrderCheck;
import com.dq.vo.ListDataVo;
import com.dq.vo.Vo;
import com.dq.domain.orders.OrgOrder;
import com.dq.domain.orders.info.OrderInfo;
import com.dq.domain.org.Org;
import com.dq.domain.sys.user.user.base.SysUser;
import com.dq.service.AllService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author
 * @date 2019/9/1716:24
 */
@Api(description = "采购单管理")
@RestController
@RequestMapping("/api/orgOrder")
public class ApiOrgOrderController extends AllService {

    @PostMapping("/add")
    @ApiOperation("采购订单添加")
    public Vo purchaseAdd(@ApiParam("productId: 产品ID非goo|dsId, num: 数量") @RequestBody List<OrgOrder.OrgOrderAdd> orgOrderAddList) throws AdminException {
        SysUser user = sysUserService.getUser();
        if(user.getSection() == null || user.getSection().getId() == null) {
            throw new AdminException("登录用户门店信息异常");
        }
        Org org = (Org) user.getSection();

        baseOrgOrderService.orgOrderAdd(orgOrderAddList, org, user);
        return new Vo(HttpStatus.OK);
    }

    @PostMapping("/list")
    @ApiOperation("采购订单列表")
    public Vo list(@ApiParam("type: (查询类型： org门店端调用， boss BOSS端)")@RequestBody OrgOrderQuery orgOrderQuery) throws AdminException {
        orgOrderQuery.format();
        if(StringUtils.isEmpty(orgOrderQuery.getType())) {
            throw new AdminException("参数type 为空");
        }
        SysUser user = sysUserService.getUser();
        Org org = null;
        Long userId = null;
        if("org".equals(orgOrderQuery.getType())) {
            if(user.getSection() == null || user.getSection().getId() == null) {
                throw new AdminException("登录账号的门店信息获取异常");
            }
            org = (Org) user.getSection();
            userId = user.getId();
        }else {
            if(orgOrderQuery.getOrgId() != null) {
                org = dao.get(Org.class, orgOrderQuery.getOrgId());
                if(org == null) {
                    throw new AdminException("无对用门店信息");
                }
            }
        }


        List<OrgOrder> orgOrderList = orgOrderService.findByWhere(orgOrderQuery, org, userId);
        Long totalCount = orgOrderService.countByWhere(orgOrderQuery, org, userId);

        List<OrgOrder.OrgOrderListVo> orgOrderListVos = new ArrayList<>();
        orgOrderList.forEach(orgOrder -> {orgOrderListVos.add(orgOrder.toOrgOrderListVo());});
        return new ListDataVo(orgOrderListVos, totalCount);
    }

    @GetMapping("/detail/{id}")
    @ApiOperation("采购订单详情商品列表")
    public Vo userOrderDetail(@PathVariable @ApiParam("订单id") Long id) throws AdminException {
        OrgOrder orgOrder = dao.get(OrgOrder.class, id);
        if (null == orgOrder) {
            throw new AdminException("采购单不存在");
        }

        List<OrderInfo> orderInfoList = orderInfoRepository.findByOrders(orgOrder);
        List<OrderInfo.OrgOrderInfoVo> orgOrderInfoVos = new ArrayList<>();
        orderInfoList.forEach(orderInfo -> {
            orgOrderInfoVos.add(orderInfo.toOrgOrderInfoVo());
        });

        OrgOrder.OrgOrderListVo basicVo = orgOrder.toOrgOrderListVo();

        basicVo.setOrgOrderInfoVos(orgOrderInfoVos);

        return new Vo(basicVo);
    }

    @ApiOperation("采购审核")
    @PostMapping("/check")
    @RequiresPermissions("sys:orgOrder:update")
    public Vo check(@RequestBody OrgOrderCheck orgOrderCheck) throws AdminException {
        if(orgOrderCheck.getId() == null) {
            throw new AdminException("缺少参数id");
        }

        OrgOrder check = orgOrderService.check(orgOrderCheck.yOrNo, orgOrderCheck.getRemake(), orgOrderCheck.getId());

        return new Vo(HttpStatus.OK);
    }

    @GetMapping("/delete/{orderId}")
    @ApiOperation("订单删除")
    public Vo orgUserDelete(@ApiParam("订单ID") @PathVariable Long orderId)throws AdminException{
        OrgOrder orgOrder = dao.get(OrgOrder.class, orderId);
        if (null == orgOrder) {
            throw new AdminException("订单不存在");
        }

        orgOrder.setDeleted(true);

        orgOrderService.update(orgOrder);
        return new Vo(HttpStatus.OK);
    }


}